<?php


$dsn="mysql:Server=127.0.0.1;dbname=blog";
$db= new PDO($dsn, "root", "123456");
$db ->exec("set names utf8mb4");

$sql ="select * from admin where admin_email='{$adminEmail}'";
//echo $sql ;
$result =$db->query($sql);
$admin =$result->fetch(PDO::FETCH_ASSOC);

if(!$admin){
    echo "账户或密码不匹配。<a href='index.php?c=login'>前往登录页面</a>";
    exit();
}

if ($admin['admin_password'] == $adminPassword) {
    if (!session_id()) {
        session_start();
    }
    $_SESSION['admin_email'] = $adminEmail;
    $_SESSION['admin_name'] = $admin['admin_name'];
    if ($rememberMe) {
        setcookie('PHPSESSID', session_id(), time() + 20);
    }

    $log = [
        'action' => 'admin_login',
        'content' => '登录后台成功',
        'time' => date("Y-m-d H:i:s", time()),
        'ip' => $_SERVER['REMOTE_ADDR'],
        'admin_email' => $adminEmail,
    ];
    $file = fopen("./logs/" . date("Y-m-d") . ".txt", 'a+');
    if ($file) {
        fwrite($file, json_encode($log, JSON_UNESCAPED_UNICODE) . PHP_EOL);
        fclose($file);
    } else {
        echo "打开文件错误";
        exit();
    }

//    $log = [
//        'action' => 'admin_login',
//        'content' => '登录后台失败',
//        'time' => date("Y-m-d H:i:s", time()),
//        'ip' => $_SERVER['REMOTE_ADDR'],
//        'admin_email' => $adminEmail,
//    ];
//    $file = fopen("./logs/" . date("Y-m-d") . ".txt", 'a+');
//    if ($file) {
//        fwrite($file, json_encode($log, JSON_UNESCAPED_UNICODE) . PHP_EOL);
//        fclose($file);
//    } else {
//        echo "打开文件错误";
//        exit();
//    }
}